package com.applink.sell_proof.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.R;
import com.applink.sell_proof.pojo.GenericObject;
import com.applink.sell_proof.utilXMLParser.AndroidUIParser;
import com.applink.sell_proof.utilXMLParser.ColumnSetting;

public class RewardStatementConfigAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context mContext;
	private ArrayList<GenericObject> mGenericObjectList = new ArrayList<GenericObject> ();
	private ArrayList<ColumnSetting> mColumnSettingList = new ArrayList<ColumnSetting>();

	public RewardStatementConfigAdapter(Context mContext, ArrayList<GenericObject> objects,ArrayList<ColumnSetting> columnSettings){
		
		mInflater = LayoutInflater.from(EMarketing.getInstance().getApplicationContext());
		this.mContext = mContext;
		this.mGenericObjectList= objects;
		this.mColumnSettingList = columnSettings;	
	}

	@Override
	public int getCount() {
		return this.mGenericObjectList.size();	
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		try {
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.redeem_item_config, null);	
				}	
		       	
		        final GenericObject object = this.mGenericObjectList.get(position);
		        if (object != null) {
	            	 // setting list_item views
		        		for(int i=0; i< mColumnSettingList.size();i++){
			        		ColumnSetting columnSetting = new ColumnSetting();
			        		columnSetting  = mColumnSettingList.get(i);
	            		    int fieldID = -1;
			        	    
	            		    // find id of Textview based on columnSetting id by reflection
	            			int idControl = AndroidUIParser.getIdFromAndroidUI(this.mContext,"redeem_item_config.xml",columnSetting.getMappingName(), "TextView");	        			    
	            			if(idControl == -1) { // this xml file doesn't have that ID control
	            			    return null;
	            		    }
	
	         			    TextView tvId	 = (TextView) convertView.findViewById(idControl);
	         			    String content = object.getContentFromObjectMappingName(columnSetting.getMappingName());
	         			    tvId.setText(content);        			  
	            	  }
		        }
					
		     /***			
  				holder.reward_type.setText(mData.get(position).getRewardType());
				holder.reward_name.setText(mData.get(position).getProgramName());
				holder.redeem_product.setText(String.valueOf((mData.get(position).getRewardAmount())));
				holder.redeem_status.setText(String.valueOf(mData.get(position).getObjectID()));
		     */			
		 } catch (Exception e) { 
	        	 System.out.println(e.toString());
	        	 convertView = null;
	      }
	         
		return convertView;
	}
	
	/***private class ChildHolder {
		TextView reward_period;
		TextView reward_program_code;
		TextView reward_category;
		TextView reward_quantity;
		TextView reward_amount; 
	}*/
}
